//
//  LJJMyHeadView.m
//  shopTest
//
//  Created by 梁佳佳 on 2016/11/14.
//  Copyright © 2016年 梁佳佳. All rights reserved.
//

#import "LJJMyHeadView.h"

@interface LJJMyHeadView ()

@property (strong, nonatomic) UIImageView *imageView;
@property (strong, nonatomic) UIButton *loginBtn;    /** 登录 **/
@property (strong, nonatomic) UIButton *registBtn;    /** 注册 **/
@property (strong, nonatomic) UIImageView *iconImageView;    /** 头像 **/
@property (strong, nonatomic) UILabel *nameLabel;    /** 用户名 **/
@property (strong, nonatomic) UILabel *levelLabel;    /** 等级 **/

@end

@implementation LJJMyHeadView

- (instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        [self addSubview:self.imageView];
        [self addSubview:self.loginBtn];
        [self addSubview:self.registBtn];
        [self addSubview:self.iconImageView];
        [self addSubview:self.nameLabel];
        [self addSubview:self.levelLabel];
    }
    return self;
}
- (void)layoutSubviews
{
    [super layoutSubviews];
    __weak typeof(self) weakSelf = self;
    
    [self.imageView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.edges.equalTo(weakSelf).with.insets(UIEdgeInsetsMake(0, 0, 40, 0));
    }];
    [self.loginBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerX.equalTo(weakSelf.mas_centerX).offset(-60);
        make.centerY.equalTo(weakSelf.imageView.mas_centerY);
        make.size.mas_equalTo(CGSizeMake(45, 23));
    }];
    [self.registBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerX.equalTo(weakSelf.mas_centerX).offset(60);
        make.centerY.equalTo(weakSelf.imageView.mas_centerY);
        make.size.mas_equalTo(CGSizeMake(45, 23));
    }];
    [self.iconImageView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.size.mas_equalTo(CGSizeMake(75, 75));
        make.centerY.equalTo(weakSelf.imageView.mas_centerY);
        make.left.equalTo(weakSelf.mas_left).offset(60);
    }];
    
    [self.nameLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(weakSelf.iconImageView.mas_right).offset(39);
        make.height.equalTo(@16);
        make.right.equalTo(weakSelf.mas_right);
        make.top.equalTo(weakSelf.iconImageView.mas_top).offset(12);
    }];
    
    [self.levelLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(weakSelf.iconImageView.mas_right).offset(39);
        make.height.equalTo(@16);
        make.right.equalTo(weakSelf.mas_right);
        make.bottom.equalTo(weakSelf.iconImageView.mas_bottom).offset(-12);
    }];
}
#pragma mark -
#pragma mark - method
- (void)loginMehthod:(UIButton *)sender
{
    self.loginBlock();
}
- (void)registMethod:(UIButton *)sender
{
    self.registBlock();
}
- (void)reloadHeadView
{
    NSDictionary *dic = [[NSUserDefaults standardUserDefaults] objectForKey:@"ISLOGIN"];
    if (dic.count) {
        self.loginBtn.hidden = YES;
        self.registBtn.hidden = YES;
        self.iconImageView.hidden = NO;
        self.nameLabel.hidden = NO;
        self.levelLabel.hidden = NO;
        self.levelLabel.text = dic[@"MemberLvl"];
        self.nameLabel.text = dic[@"MemberName"];
    }
    else
    {
        self.loginBtn.hidden = NO;
        self.registBtn.hidden = NO;
        self.iconImageView.hidden = YES;
        self.nameLabel.hidden = YES;
        self.levelLabel.hidden = YES;
    }
}
#pragma mark -
#pragma mark - lazy method
- (UIImageView *)imageView
{
    if (!_imageView) {
        _imageView = [[UIImageView alloc] init];
        _imageView.image = [UIImage imageNamed:@"我的背景"];
    }
    return _imageView;
}
- (UIButton *)loginBtn
{
    if (!_loginBtn) {
        _loginBtn = [UIButton buttonWithType:UIButtonTypeCustom];
        [_loginBtn setTitle:@"登录" forState:UIControlStateNormal];
        [_loginBtn setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
        _loginBtn.titleLabel.font = [UIFont systemFontOfSize:18.0f];
        [_loginBtn addTarget:self action:@selector(loginMehthod:) forControlEvents:UIControlEventTouchUpInside];
    }
    return _loginBtn;
}
- (UIButton *)registBtn
{
    if (!_registBtn) {
        _registBtn = [UIButton buttonWithType:UIButtonTypeCustom];
        [_registBtn setTitle:@"注册" forState:UIControlStateNormal];
        [_registBtn setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
        _registBtn.titleLabel.font = [UIFont systemFontOfSize:18.0f];
        [_registBtn addTarget:self action:@selector(registMethod:) forControlEvents:UIControlEventTouchUpInside];
    }
    return _registBtn;
}
- (UIImageView *)iconImageView
{
    if (!_iconImageView) {
        _iconImageView = [[UIImageView alloc] init];
        _iconImageView.image = [UIImage imageNamed:@"登录界面qq登陆"];
    }
    return _iconImageView;
}
- (UILabel *)nameLabel
{
    if (!_nameLabel) {
        _nameLabel = [[UILabel alloc] init];
        _nameLabel.text = @"用户名";
    }
    return _nameLabel;
}
- (UILabel *)levelLabel
{
    if (!_levelLabel) {
        _levelLabel = [[UILabel alloc] init];
        _levelLabel.text = @"用户等级";
    }
    return _levelLabel;
}
@end
